import asyncio
import datetime

import queu
import utils
from handlers import start, stop_words, require_channels, ads, users_message, admin_menu, help
from loader import bot, dp, db, scheduler, logger


async def startup(bot):
    await utils.default_command(bot)
    me = await bot.get_me()
    logger.info(f"bot started | {me.first_name} @{me.username}")


async def shut():
    logger.info("bot stopped")


async def main():
    db.create_table_()
    scheduler.start()
    context = {
        "users": db.get_all_clients(),
        'stop_words': db.select_default_word(),
        # 'chats': db.select_all_chats(),
    }
    dp.include_router(stop_words.router)
    dp.include_router(users_message.router)
    dp.include_router(require_channels.router)
    dp.include_router(ads.router)
    dp.include_router(admin_menu.router)
    dp.include_router(start.router)
    dp.include_router(help.router)
    scheduler.add_job(queu.create_tasks, trigger='interval', seconds=60, next_run_time=datetime.datetime.now())
    # scheduler.add_job(utils.validate_db_data, trigger='interval', hours=30, next_run_time=datetime.datetime.now())
    await startup(bot)
    await bot.delete_webhook()
    await dp.start_polling(bot, allowed_updates=dp.resolve_used_update_types(), context=context, skip_updates=True)


if __name__ == '__main__':
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        asyncio.run(shut())
